﻿using HalconDotNet;
using IniReadWrite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VisionRoc.Core
{
    public class OPTCamera
    {
        public OPTCamera(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            FilePath = path + "//Camera.ini";

            //判断文件是否存在
            if (File.Exists(FilePath))
            {
                Read_Parameter();
                LoPath();
            }
            else
            {
                CAMERA_STATE = 1;
                TRIGGER_MODEL = 2;
                OFFLINE_IMAGE_PATH = "";
                OFFLINE_IMAGE_FILE_PATH = "";
                CAMERA_INIT_INFO = "";
                Exposure = 1000;
                Gain = 1;
                ImageType = "Mono8";//RGB8
                Write_Parameter();
            }
        }

        /// <summary>
        /// 完整路径
        /// </summary>
        public string FilePath;

        /// <summary>
        /// 相机状态   1为离线图像状态   2为离线路径状态   3为在线相机状态
        /// </summary>
        public int CAMERA_STATE;

        /// <summary>
        /// 离线图像路径
        /// </summary>
        public string OFFLINE_IMAGE_PATH;

        /// <summary>
        /// 离线图像文件路径
        /// </summary>
        public string OFFLINE_IMAGE_FILE_PATH;

        /// <summary>
        /// 相机初始化信息
        /// </summary>
        public string CAMERA_INIT_INFO;

        /// <summary>
        /// 触发模式 外触发为1  软触发为2
        /// </summary>
        public int TRIGGER_MODEL;

        /// <summary>
        /// 曝光
        /// </summary>
        public double Exposure;

        /// <summary>
        /// 增益
        /// </summary>
        public double Gain;

        /// <summary>
        /// 图像类型
        /// </summary>
        public string ImageType;

        /// <summary>
        /// 图像文件
        /// </summary>
        public HTuple hv_ImageFiles = new HTuple();

        public void LoPath()
        {
            try
            {
                hv_ImageFiles?.Dispose();
                if (OFFLINE_IMAGE_FILE_PATH != "")
                {
                    if (Directory.Exists(OFFLINE_IMAGE_FILE_PATH))
                    {
                        HOperatorSet.ListFiles(OFFLINE_IMAGE_FILE_PATH, (new HTuple("files")).TupleConcat("follow_links"), out hv_ImageFiles);
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleRegexpSelect(hv_ImageFiles, (new HTuple("\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima)$")).TupleConcat(
                                "ignore_case"), out ExpTmpOutVar_0);
                            hv_ImageFiles.Dispose();
                            hv_ImageFiles = ExpTmpOutVar_0;
                        }
                    }
                }
            }
            catch
            {
                //Console.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 保存相机参数
        /// </summary>
        public void Write_Parameter()
        {
            try
            {
                //写
                IniAPI.INIWriteValue(FilePath, "CameraInfo", "CameraState", CAMERA_STATE.ToString());
                IniAPI.INIWriteValue(FilePath, "CameraInfo", "CameraImgaePath", OFFLINE_IMAGE_PATH);
                IniAPI.INIWriteValue(FilePath, "CameraInfo", "CameraFilePath", OFFLINE_IMAGE_FILE_PATH);
                IniAPI.INIWriteValue(FilePath, "CameraInfo", "CameraIniMessasge", CAMERA_INIT_INFO);
                IniAPI.INIWriteValue(FilePath, "CameraInfo", "TriggerModel", TRIGGER_MODEL.ToString());
                IniAPI.INIWriteValue(FilePath, "CameraInfo", "Exposure", Exposure.ToString());
                IniAPI.INIWriteValue(FilePath, "CameraInfo", "Gain", Gain.ToString());
                IniAPI.INIWriteValue(FilePath, "CameraInfo", "ImageType", ImageType.ToString());
            }
            catch { MessageBox.Show("保存失败!"); }
        }

        /// <summary>
        /// 读取相机参数
        /// </summary>
        public void Read_Parameter()
        {
            try
            {
                //读
                CAMERA_STATE = Convert.ToInt32(IniAPI.INIGetStringValue(FilePath, "CameraInfo", "CameraState", "0"));
                OFFLINE_IMAGE_PATH = IniAPI.INIGetStringValue(FilePath, "CameraInfo", "CameraImgaePath", "0");
                OFFLINE_IMAGE_FILE_PATH = IniAPI.INIGetStringValue(FilePath, "CameraInfo", "CameraFilePath", "0");
                CAMERA_INIT_INFO = IniAPI.INIGetStringValue(FilePath, "CameraInfo", "CameraIniMessasge", "0");
                TRIGGER_MODEL = Convert.ToInt32(IniAPI.INIGetStringValue(FilePath, "CameraInfo", "TriggerModel", "0"));
                Exposure = double.Parse(IniAPI.INIGetStringValue(FilePath, "CameraInfo", "Exposure", "0"));
                Gain = double.Parse(IniAPI.INIGetStringValue(FilePath, "CameraInfo", "Gain", "0"));
                ImageType = IniAPI.INIGetStringValue(FilePath, "CameraInfo", "ImageType", "0");
            }
            catch { }

        }


    }
}
